#ifndef __funcs_hpp__
#define __funcs_hpp__

#include <string>

std::string xml_escape(const std::string& str)
{
  std::string res;
  res.reserve(200);

  for(size_t i = 0; i < str.length(); ++i) {
    switch(str[i]) {
    case '"':
      res += "&quot;";
      break;
    case '\'':
      res += "&apos;";
      break;
    case '<':
      res += "&lt;";
      break;
    case '>':
      res += "&gt;";
      break;
    case '&':
      res += "&amp;";
      break;
    default:
      res += str[i]; 
    }
  }

  return res;
}

size_t find_close_par_pos(const std::string& str, size_t beg)
{
  size_t open  = 1;
  size_t close = 0;
  size_t i = beg;
  while ( open != close && i < str.length() ) {
    if( str[i] == '(' )
      ++open;
    else if( str[i] == ')' )
      ++close;

    ++i;
  }

  return (open == close ? i : 0);
}

#endif
